package intant.enums.test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapEnumTestCase {

    @Test
    public void testGetMapVo() {
        Assertions.assertTrue(EnabledStatus.ENABLED.getMapVo().containsKey(MapEnum.CODE_KEY));
        Assertions.assertTrue(EnabledStatus.ENABLED.getMapVo().containsKey(MapEnum.DESC_KEY));
        Assertions.assertEquals(EnabledStatus.ENABLED.getMapVo().get(MapEnum.CODE_KEY), EnabledStatus.ENABLED.getCode());
        Assertions.assertEquals(EnabledStatus.ENABLED.getMapVo().get(MapEnum.DESC_KEY), EnabledStatus.ENABLED.getDesc());
    }

    @Test
    public void testGetMapVo2() {
        Assertions.assertTrue(EnabledStatus.ENABLED.getMapVo("id", "name").containsKey("id"));
        Assertions.assertTrue(EnabledStatus.ENABLED.getMapVo("id", "name").containsKey("name"));
        Assertions.assertEquals(EnabledStatus.ENABLED.getMapVo("id", "name").get("id"), EnabledStatus.ENABLED.getCode());
        Assertions.assertEquals(EnabledStatus.ENABLED.getMapVo("id", "name").get("name"), EnabledStatus.ENABLED.getDesc());
    }

    @Test
    public void testList() {
        List<Map<String, Object>> selectList = MapEnum.list(EnabledStatus.class);
        Assertions.assertEquals(selectList.size(), 3);
        Map<String, Object> vo = selectList.get(0);
        Assertions.assertTrue(vo.containsKey(MapEnum.CODE_KEY));
        Assertions.assertTrue(vo.containsKey(MapEnum.DESC_KEY));
        Assertions.assertEquals(vo.get(MapEnum.CODE_KEY), EnabledStatus.ENABLED.getCode());
        Assertions.assertEquals(vo.get(MapEnum.DESC_KEY), EnabledStatus.ENABLED.getDesc());
    }

    @Test
    public void testList2() {
        List<Map<String, Object>> selectList = MapEnum.list("id", "name", EnabledStatus.class);
        Assertions.assertEquals(selectList.size(), 3);
        Map<String, Object> vo = selectList.get(0);
        Assertions.assertTrue(vo.containsKey("id"));
        Assertions.assertTrue(vo.containsKey("name"));
        Assertions.assertEquals(vo.get("id"), EnabledStatus.ENABLED.getCode());
        Assertions.assertEquals(vo.get("name"), EnabledStatus.ENABLED.getDesc());
    }

    @Test
    public void testAsList() {
        List<Map<String, Object>> selectList = MapEnum.asList(EnabledStatus.ENABLED, EnabledStatus.DISABLED);
        Assertions.assertEquals(selectList.size(), 2);
        Map<String, Object> vo = selectList.get(0);
        Assertions.assertTrue(vo.containsKey(MapEnum.CODE_KEY));
        Assertions.assertTrue(vo.containsKey(MapEnum.DESC_KEY));
        Assertions.assertEquals(vo.get(MapEnum.CODE_KEY), EnabledStatus.ENABLED.getCode());
        Assertions.assertEquals(vo.get(MapEnum.DESC_KEY), EnabledStatus.ENABLED.getDesc());
    }

    @Test
    public void testAsList2() {
        List<Map<String, Object>> selectList = MapEnum.asList("id", "name", EnabledStatus.ENABLED, EnabledStatus.DISABLED);
        Assertions.assertEquals(selectList.size(), 2);
        Map<String, Object> vo = selectList.get(0);
        Assertions.assertTrue(vo.containsKey("id"));
        Assertions.assertTrue(vo.containsKey("name"));
        Assertions.assertEquals(vo.get("id"), EnabledStatus.ENABLED.getCode());
        Assertions.assertEquals(vo.get("name"), EnabledStatus.ENABLED.getDesc());
    }

    enum EnabledStatus implements MapEnum<Integer> {
        ENABLED(1, "启用"),
        DISABLED(0, "禁用"),
        DELETED(-1, "删除")
        ;

        private final Integer code;
        private final String desc;

        EnabledStatus(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        @Override
        public Integer getCode() {
            return code;
        }

        @Override
        public String getDesc() {
            return desc;
        }

        @Override
        public Map<String, Object> getInitMap() {
            return new LinkedHashMap<>(4);
        }

    }
}
